###############################################################################
#                                                                             #
#    This file is part of the testsuite of the "ssbot" Python module.         #
#    It is distributed under the MIT License. See LICENSE.txt for details.    #
#                                                                             #
###############################################################################


import os
import os.path
import sqlite3
import sys
import unittest

sys.path.append('../')

import ssbot.ssdb
import ssbot.ssutils


class SchedulerDBTests(unittest.TestCase):
    def setUp(self):
        self.test_scheduler_name = 'test_scheduler'
        self.builder_name_list = ['builder1', 'builder2', 'builder3']
        self.db_file = self.test_scheduler_name + '.db'
        self.db = ssbot.ssdb.SchedulerDB(self.test_scheduler_name,
                                         self.builder_name_list)

    def tearDown(self):
        if os.path.exists(self.db_file):
            os.remove(self.db_file)

    def test_db_file_creation(self):
        self.assertTrue(os.path.exists(self.db_file))

    def test_get_build_set_count(self):
        build_number_map = {
            'builder1': 1,
            'builder2': 2,
            'builder3': 3
        }
        self.db.add_new_build_set(1, 'test_add_new_build', build_number_map)
        self.assertEqual(self.db.get_build_set_count(), 1)

    def test_add_new_build_set(self):
        build_number_map = {
            'builder1': 1,
            'builder2': 2,
            'builder3': 3
        }
        self.db.add_new_build_set(1, 'test_add_new_build', build_number_map)
        conn = sqlite3.connect(self.db_file)
        c = conn.cursor()
        c.execute('SELECT * FROM %s WHERE build_set_number="1"' %
                  ssbot.ssdb.SCHEDULER_TABLE_NAME)
        builds = []
        while True:
            build = c.fetchone()
            if build:
                builds.append(build)
            else:
                break
        conn.close()
        self.assertEqual(1, len(builds))
        build = builds[0]
        self.assertEqual(int(build[0]), 1)
        self.assertEqual('test_add_new_build', build[1])
        self.assertEqual(1, int(build[2]))
        self.assertEqual(2, int(build[3]))
        self.assertEqual(3, int(build[4]))

    def test_get_build_set(self):
        build_number_map = {
            'builder1': 1,
            'builder2': 2,
            'builder3': 3
        }
        self.db.add_new_build_set(1, 'test_get_build_set', build_number_map)
        build_set = self.db.get_build_set(1)
        self.assertIsNotNone(build_set)
        self.assertEqual(build_set['build_set_number'], 1)
        self.assertEqual(build_set['reason'], 'test_get_build_set')
        self.assertEqual(build_set['builder1'], 1)
        self.assertEqual(build_set['builder2'], 2)
        self.assertEqual(build_set['builder3'], 3)
        build_set = self.db.get_build_set(2)
        self.assertIsNone(build_set)


if __name__ == '__main__':
    unittest.main()
